$(function () {
    var subentryList;
    var typeList;
    var level1AreaList;
    $.ajax({
        type: 'POST',
        url: '/meter/getAgreement',
        async: false,
        data: {},
        dataType: 'json',
        success: function (result) {
            if (result.code==666){
                level1AreaList = result.data.level1AreaList;
                subentryList=result.data.subentryList;
                typeList=result.data.typeList;
            }else{
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            if (XMLHttpRequest.status==555){
                var layer;
                layui.use(['layer', 'form'], function(){
                    layer = layui.layer
                    layer.alert('未登录,请登录后操作',{icon:7}, function(index){
                        //do something
                        layer.close(index);
                        top.location.href="/login";
                    });
                });
            }else{
                var layer;
                layui.use(['layer', 'form'], function(){
                    layer = layui.layer
                    layer.alert('页面加载失败',{icon:7}, function(index){
                        layer.close(index);
                    });
                });
            }
        },
    });

layui.use('table', function(){
    var table = layui.table;

    //方法级渲染
    table.render({
        elem: '#LAY_table_user'
        ,url: "/meter/getMeterList"
        ,cols: [[
            // {field:'id', title: 'ID', width:100, }
            {field:'id', event:'Detail', title: '表计', width:400,}
            ,{field:'protocolTypeName',event:'Detail', title: '类型', width:400}
            ,{field:'subentryTypeName',event:'Detail', title: '分项',  width:300}
            ,{field:'position',event:'Detail', title: '计费区', width:400}
            ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:300}
        ]]
        ,id: 'testReload'
        ,page: true
        ,height: 600
        ,response: {
            statusCode: 666 //规定成功的状态码，默认：0
        }
        // ,even:true //每行条纹
        ,limits: [10,15,20,25,30]
        ,limit: 10 //每页默认显示的数量
        ,toolbar:'#toolbarDemo'  //导出打印
        ,parseData:function (res) {
            console.log(res);
            return{
                "code":res.code,
                "msg":res.msg,
                "count":res.data.meterList.length,
                "data":res.data.meterList
            };
        }
        , done: function(res, curr, count){
            // $(".layui-table-box").find("[data-field='id']").css("display","none");

        }

    });

    var $ = layui.$, active = {
        reload: function(){
            var demoReload = $('#demoReload');
            //执行重载
            table.reload('testReload', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                ,where: {
                        meterId: demoReload.val()
                }
            });
        }
    };

    $('.layui-btn').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    //监听行单击事件（单击事件为：rowDouble）
    //其中的user对应的是 table中的lay-filter="user"

    table.on('tool(user)', function(obj){
        var id=obj.data['id'];
        var layer;
        layui.use(['layer', 'form'], function(){
            layer = layui.layer
        });

        if(obj.event === 'Detail'){
            meterDetail(id);
        }
        if(obj.event === 'del'){
            layer.confirm('确定要删除该表计吗？', {
                btn: ['确定','取消'] //按钮
            }, function(){
                $.ajax({
                    type: 'POST',
                    url: '/meter/delMeter',
                    async: false,
                    data: {"id":id},
                    dataType: 'json',
                    success: function (result) {
                        if (result.code==666){
                            layer.alert('删除成功'+id, {icon: 1},function (index) {
                                layer.close(index);
                                window.location.reload()
                            });

                        }else{
                            layer.alert('删除失败', {icon: 2},function (index) {
                                layer.close(index);
                                window.location.reload()
                            });
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        if (XMLHttpRequest.status==555){
                            var layer;
                            layui.use(['layer', 'form'], function(){
                                layer = layui.layer
                                layer.alert('未登录,请登录后操作',{icon:7}, function(index){
                                    //do something
                                    layer.close(index);
                                    top.location.href="/login";
                                });

                            });

                        }else{
                            var layer;
                            layui.use(['layer', 'form'], function(){
                                layer = layui.layer
                                layer.alert('页面加载失败',{icon:7}, function(index){
                                    //do something
                                    layer.close(index);
                                });
                            });
                        }
                    },
                });

            }, function(){

            });

        }
    });
});
$("#add").click(function () {
    var contentStr="";
    contentStr+="   <div class=\"modleright\" style='background-color: white'>\n" +
        "<select class=\"select1\" style=\"width: 100%;height: 42px\" name=\"type\" onchange='fnx(this);' >\n" +
        "<option value=\"\" class=\"bold\">请选择协议</option>\n";
    for (i=0;i<typeList.length;i++) {
        contentStr+="<option value=\""+typeList[i].id+"\">协议类型 :"+typeList[i].name+" </option>"
    }

    contentStr+=" </select>\n" +
        " <select id='typeInfo' class=\"select1\" style=\"width: 100%;height: 42px;float: left\" name=\"info\">\n" +
       " <option value =\"\"  class=\"bold\">表计型号</option>";


    contentStr+=" </select>\n" +
        " <select class=\"select1\" style=\"width: 100%;height: 42px;float: left\" name=\"subentry\">\n" +
        " <option value =\"\"  class=\"bold\">分项类别</option>";
    for (i=0;i<subentryList.length;i++) {
        contentStr+="<option value=\""+subentryList[i].id+"\">"+subentryList[i].name+" </option>"
    }
    contentStr+=" </select>\n" +
        "<div style=\"width: 100%;height: 42px;float: left\">\n" +
        "<div class=\"name bold\" style=\"width: 10%;\">表记编号</div>\n" +
        "<input class=\"name\" name=\"code\" type=\"text\" style=\"width: 90%\">\n" +
        "</div>\n" ;
    contentStr+="<div style=\"width: 100%;height: 42px;float: left\">\n" +
        "<div class=\"name bold\" style=\"width: 10%;\">表记位置</div>\n" +
        " <select class=\"select1\" style=\"width: 28%;height: 42px;float: left\" onchange='level1Change(this);' name=\"level1Area\">\n" +
        " <option value =\"\"  class=\"bold\">请选择</option>";
    for (i=0;i<level1AreaList.length;i++) {
        contentStr+="<option value=\""+level1AreaList[i].id+"\">"+level1AreaList[i].name+" </option>"
    }
    contentStr+=" </select><span style='position: relative;left:-214px;top: 7px; font-size: 20px;'>--</span>\n" +
        " <select id='area2' class=\"select1\" style=\"width: 28%;left: 24px;position: relative;height: 42px;float: left \" onchange='level2Change(this);' name=\"level2Area\">\n" +
        " <option value =\"\"  class=\"bold\">请选择</option>";
    contentStr+=" </select><span style='position: relative;left: -2px;top: 7px;font-size: 20px;'>--</span>\n" +
        " <select id='area3' class=\"select1\" style=\"width: 28%;left: 46.5px;top:-30px;position: relative;height: 43px;float: left\" name=\"level3Area\">\n" +
        " <option value =\"\"  class=\"bold\">请选择</option>";
    contentStr+=" </select></div>\n" ;
    contentStr+="<div style=\"width: 100%;height: 42px;float: left\">\n" +
        "<div class=\"name bold\" style=\"width: 10%;\">备注</div>\n" +
        "<input class=\"name\" name=\"remark\" type=\"text\" style=\"width: 90%\">\n" +
        "</div>\n" +
        "<button class=\"btn green\" type=\"button\" onclick=\"addmeter();\" style=\"float: left;width: 80px;border-radius: 5px !important;margin-top: 20px;position: relative;left: 47%;\">添&nbsp;&nbsp;加</button>\n" +
        "</div>";
    layui.use(['layer', 'form'], function () {
        var layer = layui.layer;
        layer.open({
            type: 1,
            title: ['表计添加', 'font-size:25px;font-weight:lighter;background-color:white;height:50px;margin-top:10px'],
            // skin: 'layui-layer-rim', //加上边框
            area: ['800px', '400px'], //宽高
            content: contentStr
        });
    });

});

})
function fnx(obj) {
    var typeId = $(obj).val();
      $("#typeInfo").empty();
    $.ajax({
        type: 'POST',
        url: '/meter/getTypeInfo',
        async: false,
        data: {"typeId":typeId},
        dataType: 'json',
        success: function (result) {
            var strInfo = " <option value =\"\"  class=\"bold\">表计型号</option>";
            if (result.data.typeInfo!=null){
                var typeInfo = result.data.typeInfo;
                for (i=0;i<typeInfo.length;i++) {
                    strInfo+="<option value=\""+typeInfo[i].id+"\">协议类型 :"+typeInfo[i].name+" </option>"
                }
            }
            $("#typeInfo").append(strInfo)

        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
        }
    });


}
function level1Change(obj) {
    var level1Id = $(obj).val();
    var strInfo="<option value=''>请选择</option>"
    $("#area2").empty();
    $("#area3").empty();
    $("#area3").append(strInfo);
    $.ajax({
        type: 'POST',
        url: '/meter/getArea2',
        async: false,
        data: {"level1Id":level1Id},
        dataType: 'json',
        success: function (result) {
            if (result.data.level2AreaList!=null){
                var level2areaInfo = result.data.level2AreaList;
                for (i=0;i<level2areaInfo.length;i++) {
                    strInfo+="<option value=\""+level2areaInfo[i].id+"\">"+level2areaInfo[i].name+" </option>"
                }
            }
            $("#area2").append(strInfo)

        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
        }
    });

}
function level2Change(obj) {
    var level2Id = $(obj).val();
    var strInfo="<option value=''>请选择</option>"
    $("#area3").empty();
    $.ajax({
        type: 'POST',
        url: '/meter/getArea3',
        async: false,
        data: {"level2Id":level2Id},
        dataType: 'json',
        success: function (result) {
            if (result.data.level3AreaList!=null){
                var level3areaInfo = result.data.level3AreaList;
                for (i=0;i<level3areaInfo.length;i++) {
                    strInfo+="<option value=\""+level3areaInfo[i].id+"\">"+level3areaInfo[i].name+" </option>"
                }
            }
            $("#area3").append(strInfo)

        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
        }
    });

}
function addmeter() {
    var layer;
    layui.use(['layer', 'form'], function(){
        layer = layui.layer
    });
    var protocoltype = $("select[name='type']").val();
    var subentry = $("select[name='subentry']").val();
    var protocolnumber = $("select[name='info']").val();
    var level1Area = $("select[name='level1Area']").val();
    var level2Area = $("select[name='level2Area']").val();
    var level3Area = $("select[name='level3Area']").val();
    var position =level1Area+"-"+level2Area+"-"+level3Area;
    var code = $("input[name='code']").val();
    var remark = $("input[name='remark']").val();
    if (protocoltype==""||protocoltype==null){
        layer.msg('请选择表记协议', {icon: 2,time:2000});
    }else if (protocolnumber==""||protocolnumber==null){
        layer.msg('请选择表记型号', {icon: 2,time:2000});
    }else if (subentry==""||subentry==null){
        layer.msg('请选择分项类别', {icon: 2,time:2000});
    }else if (code==""||code==null){
        layer.msg('请填写表记编号', {icon: 2,time:2000});
    }else if (level1Area==""||level1Area==null||level2Area==null||level2Area==""||level3Area==null||level3Area==""){
        layer.msg('位置信息不完整', {icon: 2,time:2000});
    } else{
    $.ajax({
        type: 'POST',
        url: '/meter/insertMeter',
        async: false,
        data: {"protocoltype":protocoltype,"subentrytype":subentry,"protocolnumber":protocolnumber,
            "remark":remark,"id":code,"position":position},
        dataType: 'json',
        success: function (result) {
            if (result.code==666){
                layer.alert('添加成功', {icon: 1},function () {
                    /*top.location.href="addMeter.jsp";*/
                    top.location.href="/meter/meterPage";

                });

            }else{
                layer.alert('表计ID重复', {icon: 2});
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            if (XMLHttpRequest.status==555){
                var layer;
                layui.use(['layer', 'form'], function(){
                    layer = layui.layer
                    layer.alert('未登录,请登录后操作',{icon:7}, function(index){
                        //do something
                        layer.close(index);
                        top.location.href="/login";
                    });

                });

            }else{
                alert("页面加载失败");
            }
        },
    });
    }
}
function checkRate(nubmer) {
    var re = /^[0-9]+.?[0-9]*/;//判断字符串是否为数字//判断正整数/[1−9]+[0−9]∗]∗/
    if (!re.test(nubmer)) {
        return false;
    }else{
        return true;
    }
}

function meterDetail(id) {
    $.ajax({
        type: 'POST',
        url: '/meter/meterDetail',
        async: false,
        data: {"id":id},
        dataType: 'json',
        success: function (result) {
            if (result.code==666) {
                console.log(result.data);
                var subentryList2=result.data.subentryList;
                var level1AreaList=result.data.area1List;
                var level2AreaList=result.data.area2List;
                var level3AreaList=result.data.area3List;
                var typeList2=result.data.typeList;
                var areaIds=result.data.areaIds;
                var typeInfo2=result.data.typeInfo;
                var contentStr2="";
                contentStr2+="   <div class=\"modleright\" style='background-color: white'>\n" +
                    "<select class=\"select1\" style=\"width: 100%;height: 42px\" name=\"type1\" onchange='fnx(this);' >\n" +
                    "<option value=\"\" class=\"bold\">请选择协议</option>\n";
                for (i=0;i<typeList2.length;i++) {
                    if (typeList2[i].id==result.data.meter.protocoltype){
                    contentStr2+="<option value=\""+typeList2[i].id+"\" selected='selected'>协议类型 :"+typeList2[i].name+" </option>"
                    }else{
                    contentStr2+="<option value=\""+typeList2[i].id+"\">协议类型 :"+typeList2[i].name+" </option>"
                    }
                }

                contentStr2+=" </select>\n" +
                    " <select id='typeInfo' class=\"select1\" style=\"width: 100%;height: 42px;float: left\" name=\"info1\">\n" +
                    " <option value =\"\"  class=\"bold\">表计型号</option>";
                for (i=0;i<typeInfo2.length;i++) {
                    if (typeInfo2[i].id==result.data.meter.protocolnumber){
                        contentStr2+="<option value=\""+typeInfo2[i].id+"\" selected='selected'>表记型号 :"+typeInfo2[i].name+" </option>"
                    }else{
                        contentStr2+="<option value=\""+typeInfo2[i].id+"\">表记型号 :"+typeInfo2[i].name+" </option>"
                    }
                }
                contentStr2+=" </select>\n" +
                    " <select class=\"select1\" style=\"width: 100%;height: 42px;float: left\" name=\"subentry1\">\n" +
                    " <option value =\"\"  class=\"bold\">分项类别</option>";
                for (i=0;i<subentryList2.length;i++) {
                    if (subentryList2[i].id==result.data.meter.subentrytype){
                        contentStr2+="<option value=\""+subentryList2[i].id+"\" selected='selected'>"+subentryList2[i].name+" </option>"
                    }else {
                    contentStr2+="<option value=\""+subentryList2[i].id+"\">"+subentryList2[i].name+" </option>"
                    }
                }
                contentStr2+=" </select>\n" +
                    "<div style=\"width: 100%;height: 42px;float: left\">\n" +
                    "<div class=\"name bold\" style=\"width: 10%;\">表记编号</div>\n" +
                    "<input readonly='readonly' class=\"name\" name=\"code1\" type=\"text\" style=\"width: 90%\" value=\""+result.data.meter.id+"\">\n" +
                    "</div>\n";
                contentStr2+="<div style=\"width: 100%;height: 42px;float: left;\">\n" +
                    "<div class=\"name bold\" style=\"width: 10%;\">表记位置</div>\n" +
                    " <select class=\"select1\" style=\"width: 28%;height: 42px;float: left;\" onchange='level1Change(this);' name=\"level1Area\">\n" +
                    " <option value =\"\"  class=\"bold\">请选择</option>";
                for (i=0;i<level1AreaList.length;i++) {
                    if (level1AreaList[i].id==areaIds[0]){
                        contentStr2+="<option selected='selected' value=\""+level1AreaList[i].id+"\">"+level1AreaList[i].name+" </option>"
                    }else {
                    contentStr2+="<option value=\""+level1AreaList[i].id+"\">"+level1AreaList[i].name+" </option>"
                    }
                }
                contentStr2+=" </select><span style='position: relative;left:-214px;top: 7px; font-size: 20px;'>--</span>\n" +
                    " <select id='area2' class=\"select1\" style=\"width: 28%;left: 24px;position: relative;height: 42px;float: left \" onchange='level2Change(this);' name=\"level2Area\">\n" +
                    " <option value =\"\"  class=\"bold\">请选择</option>";
                for (i=0;i<level2AreaList.length;i++) {
                    if (level2AreaList[i].id == areaIds[1]) {
                        contentStr2 += "<option selected='selected' value=\"" + level2AreaList[i].id + "\">" + level2AreaList[i].name + " </option>"
                    } else{
                        contentStr2 += "<option value=\"" + level2AreaList[i].id + "\">" + level2AreaList[i].name + " </option>"
                    }
                }
                contentStr2+=" </select><span style='position: relative;left: -2px;top: 7px;font-size: 20px;'>--</span>\n" +
                    " <select id='area3' class=\"select1\" style=\"width: 28%;left: 46.5px;top:-30px;position: relative;height: 43px;float: left\" name=\"level3Area\">\n" +
                    " <option value =\"\"  class=\"bold\">请选择</option>";
                for (i=0;i<level3AreaList.length;i++) {
                    if (level3AreaList[i].id == areaIds[2]) {
                        contentStr2 += "<option selected='selected' value=\"" + level3AreaList[i].id + "\">" + level3AreaList[i].name + " </option>"
                    } else {
                        contentStr2 += "<option value=\"" + level3AreaList[i].id + "\">" + level3AreaList[i].name + " </option>"
                    }
                }
                contentStr2+=" </select></div>\n" ;
                contentStr2+= "<div style=\"width: 100%;height: 42px;float: left\">\n" +
                    "<div class=\"name bold\" style=\"width: 10%;\">备注</div>\n" +
                    "<input class=\"name\" name=\"remark1\" type=\"text\" style=\"width: 90%\" value=\""+(result.data.meter.remark==null?"":result.data.meter.remark)+"\">\n" +
                    "</div>\n" +
                    "<button class=\"btn red\" type=\"button\" onclick=\"concelbtn();\" style=\"float: left;width: 80px;border-radius: 5px !important;margin-top: 20px;position: relative;left: 40%;\">取&nbsp;&nbsp;消</button>\n" +
                    "<button class=\"btn yellow\" type=\"button\" onclick=\"modifymeter();\" style=\"float: left;width: 80px;border-radius: 5px !important;margin-top: 20px;position: relative;left: 45%;\">修&nbsp;&nbsp;改</button>\n" +
                    "</div>";
                layui.use(['layer', 'form'], function () {
                    var layer = layui.layer;
                    layer.open({
                        type: 1,
                        title: ['表计详情', 'font-size:25px;font-weight:lighter;background-color:white;height:50px;margin-top:10px'],
                        // skin: 'layui-layer-rim', //加上边框
                        area: ['800px', '400px'], //宽高
                        content: contentStr2
                    });
                });
            }else{
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            if (XMLHttpRequest.status==555){
                var layer;
                layui.use(['layer', 'form'], function(){
                    layer = layui.layer
                    layer.alert('未登录,请登录后操作',{icon:7}, function(index){
                        //do something
                        layer.close(index);
                        top.location.href="/login";
                    });

                });

            }else{
                alert("页面加载失败");
            }
        },
    });
}
function concelbtn() {
    var layer;
    layui.use(['layer', 'form'], function(){
        layer = layui.layer
            //do something
        layer.closeAll();
        });

}
function modifymeter() {
    var layer;
    layui.use(['layer', 'form'], function(){
        layer = layui.layer
    });
    var protocoltype = $("select[name='type1']").val();
    var subentry = $("select[name='subentry1']").val();
    var protocolnumber = $("select[name='info1']").val();
    var level1Area = $("select[name='level1Area']").val();
    var level2Area = $("select[name='level2Area']").val();
    var level3Area = $("select[name='level3Area']").val();
    var position =level1Area+"-"+level2Area+"-"+level3Area;

    var code = $("input[name='code1']").val();
    var remark = $("input[name='remark1']").val();
    if (protocoltype==""||protocoltype==null){
        layer.msg('请选择表记协议', {icon: 2,time:2000});
    }else if (protocolnumber==""||protocolnumber==null){
        layer.msg('请选择表记型号', {icon: 2,time:2000});
    }else if (subentry==""||subentry==null){
        layer.msg('请选择分项类别', {icon: 2,time:2000});
    }else if (code==""||code==null){
        layer.msg('请填写表记编号', {icon: 2,time:2000});
    }else if (level1Area==""||level1Area==null||level2Area==null||level2Area==""||level3Area==null||level3Area==""){
        layer.msg('位置信息不完整', {icon: 2,time:2000});
    } else{
        $.ajax({
            type: 'POST',
            url: '/meter/modifyMeter',
            async: false,
            data: {"protocoltype":protocoltype,"subentrytype":subentry,"protocolnumber":protocolnumber,
                "remark":remark,"id":code,"position":position},
            dataType: 'json',
            success: function (result) {
                if (result.code==666){
                    layer.alert('保存成功', {icon: 1},function () {
                        top.location.href="/meter/meterPage";
                    });

                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                if (XMLHttpRequest.status==555){
                    var layer;
                    layui.use(['layer', 'form'], function(){
                        layer = layui.layer
                        layer.alert('未登录,请登录后操作',{icon:7}, function(index){
                            //do something
                            layer.close(index);
                            top.location.href="/login";
                        });

                    });

                }else{
                    alert("页面加载失败");
                }
            },
        });
    }
}
